package com.weilele.mvvm.widget

import android.content.Context
import android.util.AttributeSet
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.VirtualLayoutManager
import com.weilele.mvvm.MvvmConf
import com.weilele.mvvm.utils.safeGet

/**
 * 结合vLayout使用
 * 一切操作通过vAdapter来操作
 *
 *  layoutManager不需要设置itemCount，实际上在DelegateAdapter中会重新赋值为adapter.getItemCount()
 *  如果是自定义继承VirtualLayoutAdapter，需要设置itemCount
 *
 *  建议优先使用[BaseRecyclerView],因为多次刷新，总是会导致莫名崩溃问题，
 *  复杂布局建议再使用
 */
open class BaseVLayoutView : BaseRecyclerView {
    private val vManager by lazy { VirtualLayoutManager(context) }

    var hasConsistItemType: Boolean = false

    val vAdapter by lazy {
        val manager = when (val oldManager = layoutManager) {
            null -> {
                layoutManager = vManager
                vManager
            }
            is VirtualLayoutManager -> {
                oldManager
            }
            else -> {
                throw IllegalArgumentException("需要一个VirtualLayoutManager类型的管理器")
            }
        }
        //hasConsistItemType 不同的helper是否可以共用 viewType
        //根据自己的需求来设置
        DelegateAdapter(manager, hasConsistItemType).apply {
            super.setAdapter(this)
        }
    }
    val adaptersCount: Int
        get() = vAdapter.adaptersCount

    val vLayoutManager: VirtualLayoutManager?
        get() = layoutManager.safeGet<VirtualLayoutManager>()

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )


    override fun setLayoutManager(layout: LayoutManager?) {
        if (layout != null && layout !is VirtualLayoutManager) {
            if (MvvmConf.IS_DEBUG) {
                throw IllegalArgumentException("如果不想使用VLayoout的功能，请使用普通的RecyclerView")
            } else {
                return
            }
        }
        super.setLayoutManager(layout)
    }

    override fun setAdapter(adapter: Adapter<*>?) {
        if (MvvmConf.IS_DEBUG) {
            throw IllegalArgumentException("如果不想使用VLayoout的功能，请使用普通的RecyclerView")
        }
    }

    fun addAdapter(adapter: DelegateAdapter.Adapter<*>) {
        vAdapter.addAdapter(adapter)
    }


    fun addAdapter(position: Int, adapter: DelegateAdapter.Adapter<*>) {
        vAdapter.addAdapter(position, adapter)
    }

    fun addAdapters(adapter: MutableList<DelegateAdapter.Adapter<*>>) {
        vAdapter.addAdapters(adapter)
    }

    fun addAdapters(position: Int, adapter: MutableList<DelegateAdapter.Adapter<*>>) {
        vAdapter.addAdapters(position, adapter)
    }

    fun setAdapters(adapter: MutableList<DelegateAdapter.Adapter<*>>) {
        vAdapter.setAdapters(adapter)
    }

    fun setAdapters(adapter: DelegateAdapter.Adapter<*>) {
        vAdapter.setAdapters(listOf(adapter))
    }

    fun removeAdapter(position: Int) {
        vAdapter.removeAdapter(position)
    }

    fun removeAdapter(adapter: DelegateAdapter.Adapter<*>?) {
        vAdapter.removeAdapter(adapter)
    }

    fun removeAdapter(adapter: MutableList<DelegateAdapter.Adapter<*>>?) {
        vAdapter.removeAdapters(adapter)
    }

    fun removeFirstAdapter() {
        vAdapter.removeFirstAdapter()
    }

    fun removeLastAdapter() {
        vAdapter.removeLastAdapter()
    }
}